home *** CD-ROM | disk | FTP | other *** search
- //created by Steve Thorson
- //widgets@stephenthorson.com
-
- //variables
- var xSize = 255; //size of window
- var maxIndex = 1; //size of storage array
- var trackSection = Math.floor(xSize/8);
- var time = 0;
- var wholeOutA = 0;
- var notWholeOutB = 0;
- var dividend = 0;
- var turbo = 1;
- var spinInterval = 0;
- var lowestA = 0;
- var lowestA2 = 0;
- var intervalId = null;
- var origin = (trackSection*4);
- var stretch = origin;
- var x = origin;
- var units = "pixels";
- var backOnTrack = null;
- var inputColor = "black";
- var imageManipulation = false;
- var doneButton;
- var fadeLoading = 0;
- var finishedLoading = 0;
- var fadeLoading = 0;
- var dontLeaveYet = 0;
- var inchInc;
-
- //Initial setup
- function setup(){
- updateCheck();
- if(null != document.getElementById("version")){
- document.getElementById("version").innerText = "v"+version();
- }
- document.form.InA.value = 640;
- document.form.InB.value = 480;
- document.onmouseup = up;
- document.getElementById("slider").style.left = origin+"px";
- newRatio();
- shackleRoll = new Image();
- shackleRoll.src = "btnShackle_ovr.png";
- shackleReg = new Image("btnShackle.png");
- shackleReg.src = "btnShackle.png";
- clearRoll = new Image("btnClear_ovr.png");
- clearRoll.src = "btnClear_ovr.png";
- clearReg = new Image("btnClear.png");
- clearReg.src = "btnClear.png";
- inputs = document.getElementsByTagName("input");
- doneButton = new AppleGlassButton(document.getElementById("doneButton"), "Done", doFlipToFront);
- }
-
- //bug fix for blank input fields
- function checkBlanks(){
- if(document.form.InA.value == ''){
- document.form.InA.value = 1;
- }
- if(document.form.InB.value == ''){
- document.form.InB.value = 1;
- }
- }
-
- //new Ratio
- function newRatio(skipSetValue) {
- document.getElementById("xValue").innerText = document.form.InA.value;
- document.getElementById("yValue").innerText = document.form.InB.value;
- if(document.form.InA.value < 0 || isNaN(document.form.InA.value)){
- document.form.InA.value = "";
- }
- if(document.form.InB.value < 0 || isNaN(document.form.InB.value)){
- document.form.InB.value = "";
- }
- wholeOutA = 1;
- dividend = (document.form.InA.value/document.form.InB.value);
- notWholeOutB = roundNumber((wholeOutA/dividend),10);//first attempt
- if(document.form.InA.value != 0 && document.form.InB.value != 0 && document.form.InA.value != "" && document.form.InB.value != ""){
- while (roundNumber(notWholeOutB,10) !== Math.floor(notWholeOutB)) {
- wholeOutA++;
- notWholeOutB = wholeOutA/dividend;
- // if(wholeOutA > (document.form.InA.value / 2)){//speeds up process a little but causes bug when dealing with decimal in numerator
- // wholeOutA = Number(document.form.InA.value)-1;
- // notWholeOutB = Number(document.form.InB.value);
- // }
- }
- lowestA = wholeOutA;
- if(imageManipulation == true){
- setThumbnailSize();
- if(document.getElementById("width").checked){
- document.form.OutA.value = document.prefsForm.defaultSize.value;
- document.form.OutB.value = roundNumber((document.form.OutA.value / dividend),3);
- }else{
- document.form.OutB.value = document.prefsForm.defaultSize.value;
- document.form.OutA.value = roundNumber((document.form.OutB.value * dividend),3);
- }
- manualFix();
- }else{
- document.form.OutA.value = wholeOutA;
- document.form.OutB.value = roundNumber(notWholeOutB,3);
- }
- }
- }
-
- function setThumbnailSize(){
- if(picture.height >= (picture.width * 0.75)){ //tall picture
- document.images['thumbnail'].height = 78;
- document.images['thumbnail'].width = (78 * dividend);
- }else{ //wide picture
- document.images['thumbnail'].width = 104;
- document.images['thumbnail'].height = (104 / dividend);
- }
- }
-
- //Click on Slider
- function down(){
- wholeNumberize();
- intervalId = setInterval("grease()",50);
- document.onmousemove = mouseMove;
- }
-
- //sets found ratio to nearest whole number when you click on the slider
- function wholeNumberize(){
- if(backOnTrack !== null){
- document.form.OutA.value = backOnTrack;
- document.form.OutB.value = document.form.OutA.value/dividend;
- backOnTrack = null;
- if(document.form.OutA.value < lowestA){
- document.form.OutA.value = lowestA;
- document.form.OutB.value = document.form.OutA.value/dividend;
- }
- }
- }
-
- //Let go of slider
- function up(){
- clearInterval(intervalId);
- document.onmousemove = null;
- document.getElementById("slider").style.left = origin+"px";
- stretch = origin;
- x = origin;
- megapixelCheck();
- }
-
- //Manual ratio inquiry
- function manualA(){
- if(document.form.OutA.value < 0 || isNaN(document.form.OutA.value)){
- document.form.OutA.value = "";
- }
- if(units == "inches"){
- document.form.OutB.value = roundNumber((document.form.OutA.value/dividend), 2);
- }else{
- document.form.OutB.value = roundNumber((document.form.OutA.value/dividend),3);
- manualFix();
- }
- megapixelCheck();
- }
- function manualB(){
- if(document.form.OutB.value < 0 || isNaN(document.form.OutB.value)){
- document.form.OutB.value = "";
- }
- if(units == "inches"){
- document.form.OutA.value = roundNumber((document.form.OutB.value*dividend), 2);
- }else{
- document.form.OutA.value = roundNumber((document.form.OutB.value*dividend), 3);
- manualFix();
- }
- megapixelCheck();
- }
- function manualFix(){ //does not effect what is currently displayed but makes whole number ratio in background to get slider back on track
- wholeOutA = Math.floor(document.form.OutA.value);
- notWholeOutB = wholeOutA/dividend;//first attempt
- while (notWholeOutB !== Math.floor(notWholeOutB)) {
- wholeOutA++;
- notWholeOutB = wholeOutA/dividend;
- }
- backOnTrack = wholeOutA;
- }
-
- //bug fix for common javascript math bug
- function roundNumber(rnum,rlength) {
- //rnum = number to round
- //rlength = The number of decimal places to round to
- if (rnum > 8191 && rnum < 10485) {//fix for known javascript bug
- rnum = rnum-5000;
- var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
- newnumber = newnumber+5000;
- } else {
- var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
- }
- return(newnumber);
- }
-
-
- //slider math
- function grease(){
- stretch = ((((x - stretch) * 0.3) + stretch)); //stretch is the value of the slider div. x is the mouse value.
- document.getElementById("slider").style.left = stretch+"px";
- if(stretch < 30){ //sets boundaries for slider
- document.getElementById("slider").style.left = "30px";
- }else if(stretch > ((trackSection*8)-30)){ //sets boundaries for slider
- document.getElementById("slider").style.left = ((trackSection*8)-30)+"px";
- }
- if(stretch <= trackSection){//neg4
- time+=100;
- turbo = 7;
- section=-4;
- }else if(stretch >= trackSection && stretch < (trackSection*2)){//neg3
- time+=50;
- section=-3;
- turbo = 2;
- }else if(stretch > (trackSection*2) && stretch <= (trackSection*3)){//neg2
- time+=25;
- section=-2;
- turbo = 1;
- }else if(stretch > (trackSection*3) && stretch < origin){//neg1
- time+=10;
- section=-1;
- turbo = 1;
- }else if(stretch == origin){//0
- section=0;
- turbo = 1;
- time=0;
- }else if(stretch > origin && stretch < (trackSection*5)){//pos1
- time+=10;
- section=1;
- turbo = 1;
- }else if(stretch >= (trackSection*5) && stretch < (trackSection*6)){//pos2
- time+=25;
- section=2;
- turbo = 1;
- }else if(stretch >= (trackSection*6) && stretch < (trackSection*7)){//pos3
- time+=50;
- section=3;
- turbo = 2;
- }else if(stretch >= (trackSection*7)){//pos4
- time+=100;
- turbo = 7;
- section=4;
- }
- if(section < 0){
- lowestA2 = lowestA * -1;
- inchInc = -0.01;
- }else{
- lowestA2 = lowestA;
- inchInc = 0.01;
- }
- if(time>=100 || time<=-100){
- // if(units == "inches"){
- // document.form.OutA.value = Number(document.form.OutA.value) + (inchInc * turbo);
- // document.form.OutB.value = roundNumber(Number(document.form.OutA.value)/dividend,2);
- // if(Number(document.form.OutA.value) <= 0 || Number(document.form.OutB.value) <= 0){
- // document.form.OutA.value = (0.01 * turbo);
- // document.form.OutB.value = roundNumber(Number(document.form.OutA.value)/dividend,2);
- // }
- // if(document.form.OutA.value.length > 8 || document.form.OutB.value.length > 8){
- // document.form.OutA.value = Number(document.form.OutA.value) - (inchInc * turbo);
- // document.form.OutB.value = roundNumber(Number(document.form.OutA.value)/dividend,2);
- // }
- // }else{
- document.form.OutA.value = Number(document.form.OutA.value) + (lowestA2 * turbo);
- if(document.form.OutA.value < lowestA){
- document.form.OutA.value = lowestA;
- }
- document.form.OutB.value = roundNumber((document.form.OutA.value/dividend),3);
- if(document.form.OutA.value.length > 8 || document.form.OutB.value.length > 8){
- document.form.OutA.value = Number(document.form.OutA.value) - (lowestA2 * turbo);
- document.form.OutB.value = roundNumber((document.form.OutA.value/dividend),3);
- }
- // }
- time=0;
- }
- }
-
- //MegapixelCheck
- function megapixelCheck(){
- var temp_A = Number(document.form.OutA.value);
- var temp_B = Number(document.form.OutB.value);
- if(units == "inches"){
- temp_A *= 300;
- temp_B *= 300;
- }
- if((temp_A * temp_B) > 10000000 && imageManipulation == true){//checks to see if image is less than 10 megapixels
- if(inputColor == "black"){
- document.images['shackle'].style.opacity = .5;
- document.getElementById('resizeStopper').style.display = "block";
- for(i=0; i<inputs.length; i++){
- input = inputs[i];
- input.style.color = "FF0000";
- inputColor = "red";
- }
- }
- }else{
- if(inputColor == "red"){
- document.images['shackle'].style.opacity = 1;
- document.getElementById('resizeStopper').style.display = "none";
- for(i=0; i<inputs.length; i++){
- input = inputs[i];
- input.style.color = "000000";
- inputColor = "black";
- }
- }
- }
- }
-
- //sliding slider
- function mouseMove(e) {
- var pageX = e.pageX;
- if(pageX != -1){ //fix for safari
- x=pageX-7;
- }
- }
-
-
- //clears current thumbnail
- function clearImage(fromWhere){
- imageManipulation = false;
- megapixelCheck();
- document.getElementById("disabledFields").style.display = "none";
- document.getElementById("inputs").style.display = "block";
- document.getElementById("buttons").style.display = "none";
- document.getElementById("drag").style.display = "block";
- document.images['clear'].src=clearReg.src;
- document.images['thumbnail'].width = 104;
- document.images['thumbnail'].height = 78;
- if(fromWhere == "invalidFile"){
- document.getElementById("drag").src = "btnInvalid.png";
- document.images['thumbnail'].src = "unsupported.jpg";
- document.getElementById("thumbnail").style.display = "block";
- }else if(fromWhere == "dragenter"){
- document.getElementById("drag").src = "btnDrag.png";
- }else if (fromWhere == "credits"){
- document.images['thumbnail'].src = ".credits.jpg";
- }else{
- document.getElementById("thumbnail").style.display = "none";
- document.images['thumbnail'].src="spacer.gif";
- }
- }
-
- //on drop event
- function dragdrop (event) {
- uri = event.dataTransfer.getData("text/uri-list")
- uri = unescape(uri).replace(/file:\/\/localhost/g,'');
- slash = uri.lastIndexOf('/');
- extension = uri.substring(uri.lastIndexOf('.'));
- filename = uri.substring(slash, uri.lastIndexOf('.'));
- directory = uri.substring(0, slash);
- switch(extension.toLowerCase()){
- case ".jpeg" :
- validFile = true;
- break;
- case ".jpg" :
- validFile = true;
- break;
- case ".gif" :
- validFile = true;
- break;
- case ".tif" :
- validFile = true;
- break;
- case ".tiff" :
- validFile = true;
- break;
- case ".psd" :
- validFile = true;
- break;
- case ".png" :
- validFile = true;
- break;
- case ".pict" :
- validFile = true;
- break;
- case ".bmp" :
- validFile = true;
- break;
- default :
- validFile = false;
- break;
- }
- if(/\n/.test(uri)){ //if more than one file
- validFile = false;
- }
- if(filename == "/shackle"){
- clearImage("credits");
- }else if(validFile == true){
- picture = new Image();
- picture.onload = photoLoaded;
- picture.src = (uri);
-
- }else{
- clearImage("invalidFile");
- }
- }
-
- //Dropped image is loaded
- function photoLoaded(){
- imageManipulation = true;
- document.form.InA.value = picture.width;
- document.form.InB.value = picture.height;
- newRatio();
- document.getElementById("inputs").style.display = "none";
- document.getElementById("disabledFields").style.display = "block";
- document.getElementById("thumbnail").style.display = "block";
- document.images['thumbnail'].src=picture.src
- document.getElementById("drag").style.display = "none";
- document.getElementById("buttons").style.display = "block";
- }
-
- //function that actually resizes the image
- function resize(){
- if(document.prefsForm.jpgconvert.checked){
- useExtension = ".jpg";
- convertToJpg = '-s format jpeg ';
- }else{
- useExtension = extension;
- convertToJpg = '';
- }
- if(units == "inches"){
- var inch_width = document.form.OutA.value;
- var inch_height = document.form.OutB.value;
- var width = inch_width * 300;
- var height = inch_height * 300;
- var obj = widget.system('/usr/bin/sips ' +convertToJpg +' -s dpiHeight 300 -s dpiWidth 300 -z ' +height +' ' +width +' "' +uri +'" --out "' +directory +filename +'_' +inch_width +'x' +inch_height+'in.shkl' +useExtension +'"', null);
- }else{
- var width = Math.round(document.form.OutA.value);
- var height = Math.round(document.form.OutB.value);
- var obj = widget.system('/usr/bin/sips ' +convertToJpg +'-z ' +height +' ' +width +' "' +uri +'" --out "' +directory +filename +'_' +width +'x' +height+'.shkl' +useExtension +'"', null);
- }
- }
-
- //on drag in event
- function dragenter (event) {
- event.stopPropagation();
- event.preventDefault();
- clearImage('dragenter');
- }
-
- function dragover (event) {
- event.stopPropagation();
- event.preventDefault();
- clearInterval(dontLeaveYet);
- document.images['thumbnail'].src = "over.jpg";
- document.getElementById("thumbnail").style.display = "block";
- }
- //on drag out event
- function dragleave (event) {
- event.stopPropagation();
- event.preventDefault();
- clearInterval(dontLeaveYet);
- dontLeaveYet = setInterval(timeToLeave, 300);
- }
-
- function timeToLeave(){
- clearInterval(dontLeaveYet);
- document.getElementById("thumbnail").style.display = "none";
- }
-
- //loading screen
- function beginFade(){
- clearInterval(fadeLoading);
- clearInterval(finishedLoading);
- clearInterval(fadeLoading);
- document.getElementById("loading").style.display = "block";
- i=0;
- fadeLoading = setInterval(fadeLoadingIn, 50);
- }
-
- function hideLoading(){
- clearInterval(finishedLoading);
- i= (7);
- fadeLoading=setInterval(fadeLoadingOut, 50);
- }
-
- function fadeLoadingIn(){
- if(i < 8){
- document.getElementById("loading").style.opacity = "."+i;
- i++;
- }else{
- clearInterval(fadeLoading);
- //document.getElementById(id).style.opacity = 1;
- resize(); //resize the file
- finishedLoading=setInterval(hideLoading, 500);
- }
- }
-
- function fadeLoadingOut(){
- if(i > 0){
- document.getElementById("loading").style.opacity = "."+i;
- i--;
- }else{
- document.getElementById("loading").style.display = "none";
- clearInterval(fadeLoading);
- }
- }
- function labelClick(event, id)
- {
- id.checked = !id.checked;
- }
- function changeUnit(){
- if (document.getElementById('units1').innerText == "px"){
- document.getElementById('units1').innerText="in";
- document.getElementById('units2').innerText="in";
- document.getElementById('sliderOverlay').style.display = 'block';
- document.form.OutA.value = roundNumber(document.form.OutA.value / 300, 2);
- document.form.OutB.value = roundNumber(document.form.OutA.value / dividend, 2);
- units = "inches";
-
- }else{
- document.getElementById('units1').innerText="px";
- document.getElementById('units2').innerText="px";
- document.getElementById('sliderOverlay').style.display = 'none';
- document.form.OutA.value = roundNumber(document.form.OutA.value * 300, 3);
- document.form.OutB.value = roundNumber(document.form.OutA.value / dividend, 3);
- units = "pixels";
- manualFix();
- }
- }
-
- function updateCheck(){
- var XMLHttpRequest_object = new XMLHttpRequest();
- XMLHttpRequest_object.open("GET", "http://www.cre8tivegroup.com/widgets/index.php?version=" +version());
- XMLHttpRequest_object.onreadystatechange = function(){
- if(XMLHttpRequest_object.readyState == 4 && XMLHttpRequest_object.status == 200){
- if(XMLHttpRequest_object.responseText == "update"){
- document.getElementById('front').style.display = 'none';
- document.getElementById('needUpdate').style.display = 'block';
- }
- }
- }
- XMLHttpRequest_object.send(null);
- }